--
-- Cambio en la determinacion del caso de ganancias para liquidacion por conceptos.
--
CREATE OR REPLACE VIEW public.vmovcomlb
 AS
 SELECT comm.id AS idcomm,
    commov.tipomov,
    commov.idctb,
    commov.id,
        CASE
            WHEN artc.codartr::text = 'IVA0'::text AND commov.piva = 0.00 THEN 0.01
            ELSE commov.piva
        END AS piva,
    commov.orden,
    comm.idpadfiscal AS idpad,
    comm.fecha,
    comm.fechaemision,
    comm.tipocom,
    comm.letra,
    comm.nroptovta,
    comm.nro,
    comm.nrofinal,
    comm.nroserie,
    comm.detalle,
    comm.codambsuc,
    comm.codpadiva,
    comm.razonsocialfiscal,
    comm.tipocuitfiscal,
    comm.cuitfiscal,
    comm.codcai,
    comm.vtocai,
    comm.tipocai,
    comm.afipcbtetipo,
        CASE
            WHEN padrig_concepto.id IS NOT NULL THEN padrig_concepto.id
            ELSE comm.idpadrig
        END AS idpadrig,
    cl.codctblib,
    commov.signo AS signopse,
    commov.impo AS impopse,
    commov.impo * cl.signo::numeric AS impomov,
    commov.signo * cl.signo AS signomov,
        CASE
            WHEN artc.codartr::text = 'IVA0'::text AND commov.alicuota = 0.00 THEN 0.01
            ELSE commov.alicuota
        END AS alicuota,
    conm.base,
    conm.id AS idconcep
   FROM ctblibcom cl
     JOIN comm ON cl.tipocom::text = comm.tipocom::text
     LEFT JOIN commov ON comm.id = commov.idcomm
     LEFT JOIN conm ON conm.id = commov.id AND commov.tipomov::text = 'CON'::text
     LEFT JOIN pad ON pad.id = comm.idpad
     LEFT JOIN ( SELECT comm_1.id AS idcomm,
            max(padrig.id) AS id
           FROM comm comm_1
             JOIN conm conm_1 ON conm_1.idcomm = comm_1.id
             JOIN pad pad_1 ON pad_1.id = comm_1.idpad
             JOIN padrig ON pad_1.esinscganancias::text = padrig.esinscripto::text AND padrig.idconcep = conm_1.idcon
          WHERE pad_1.esinscganancias::text = padrig.esinscripto::text
          GROUP BY comm_1.id) padrig_concepto ON comm.id = padrig_concepto.idcomm
     LEFT JOIN artm ON artm.id = commov.id AND commov.tipomov::text = 'ART'::text
     LEFT JOIN artc ON artc.tipoarta::text = 'IVA'::text AND artc.codartr::text = 'IVA0'::text AND artm.idart = artc.idart;

ALTER TABLE public.vmovcomlb
    OWNER TO abacouser;

GRANT ALL ON TABLE public.vmovcomlb TO abacouser;

--
SELECT aaversion('497.02','497.01');
--

